
package com.iiitb.wtp;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class Parser {

	String pageSource = null;
	// Celebrity celeb = new Celebrity();
	int count = 0;

	public Map<String, String> parse(String contentOfPage, String url,
			String tag, Map<String, String> urlList) throws SQLException,
			IOException {

		// Find if there are any hyper links. Add it to the crawl frontier.
		urlList = hyperlinksExtractor(url, contentOfPage, tag, urlList);
		
		return urlList;

	}

	
	private Map<String, String> hyperlinksExtractor(String url,
			String contentOfPage, String tag, Map<String, String> urlLink)
			throws SQLException, IOException {
		String dbUrl = "jdbc:mysql://localhost:3306/";
		String dbName = "imagedb";
		String driver = "com.mysql.jdbc.Driver";
		String userName = "root";
		String password = "root";
		WebGraph wg = new WebGraph();
		try {
			Class.forName(driver).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = DriverManager.getConnection(dbUrl + dbName, userName,
				password);
		// conn =
		// DriverManager.getConnection("jdbc:mysql://localhost:3306/imagedb");
		String sql = "INSERT INTO searched_url(hyperlink, category) VALUES(?, ?)";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		String link;
		String[] ref = contentOfPage.split("href=\"http://");
	String temp1[], temp2[];
		for (int i = 1; i < ref.length; i++) {
			temp1 = ref[i].split("\"");
			link = "http://" + temp1[0];

			try {
				// try if it is correct URL by connecting it.
				URL connect = new URL(link);
				URLConnection yc = connect.openConnection();

				// Check if the URL is already in DB otherwise insert it

				pstmt.setString(1, link);
				pstmt.setString(2, tag);
				pstmt.executeUpdate();

				// Also keep the url in the arraylist
				urlLink.put(link, tag);
				//urlList.put(link, tag);
				wg.addLink(url, link, 1.0);

			} catch (MySQLIntegrityConstraintViolationException e2) {
				e2.printStackTrace();
			} catch (Exception e1) {
				e1.printStackTrace();
			}

			// }

		}
		conn.close();
		return urlLink;

	}

	private boolean humanOrNotExtractor(String contentOfPage) {
		WordFreqCounter wfc = new WordFreqCounter();
		boolean human = wfc.parse(contentOfPage);
		return human;

	}

}
